<?php
namespace addons\TfFutures\api\controllers;


use addons\TfFutures\common\models\Member;
use addons\TfFutures\common\models\Symbol;
use api\controllers\OnAuthController;
use common\helpers\ResultDataHelper;
use Yii;


class SelfCheckController extends OnAuthController
{
    public $modelClass = Symbol::class;

    /**
     * 不用进行登录验证的方法
     * 例如： ['index', 'update', 'create', 'view', 'delete']
     * 默认全部需要验证
     *
     * @var array
     */
    protected $optional = ['recommend','get-one','sym-list','list','add','pending-to-position'];

    /**
     * 添加自选
     */
    public function actionAdd(){
        if (!Yii::$app->request->isPost) {
            return ResultDataHelper::api(304,'access done!');
        }
        $token = Yii:: $app->request->get('access-token');
        if(empty($token)){
        	return ResultDataHelper::api(422,'请先登录');
        }
        $uid = Yii::$app->user->identity->member_id;
        if (empty($uid)) {
            return ResultDataHelper::api(422,'用户id不能为空');
        }
        list($memberc,$member) = Member::member($uid);
        if (empty($memberc)) {
            return ResultDataHelper::api(422,'用户不存在');
        }
        $id = Yii::$app->request->post('symbol_id');
        if (empty($id)) {
            return ResultDataHelper::api(422,'添加的产品id不能为空');
        }
        $symbol = Symbol::findOne($id);
        if(empty($symbol)){
        	return ResultDataHelper::api(422,'产品不存在');
        }
        $symbolarr = explode(',',$member->symbol);
        if(in_array($id,$symbolarr)){
            return ResultDataHelper::api(422,'您已经添加过该产品');
        }
        $idarr = explode(',',$id);
        $newarr = array_unique(array_merge($symbolarr,$idarr));
        
          
        if($newarr[0]==null){
          	unset($newarr[0]);
          }
          //print_r($newarr);die;
        $member->symbol = implode(',',$newarr);
        if (!$member->save()) {
            return ResultDataHelper::api(422, $this->analyErr($member->getFirstErrors()));
        }
        return ResultDataHelper::api(200,'添加成功');
    }

    /**
     * 删除自选
     */
    public function actionDeleteCheck(){
        if (!Yii::$app->request->isPost) {
            return ResultDataHelper::api(304,'access done!');
        }
        $uid = Yii::$app->user->identity->member_id;
        if (empty($uid)) {
            return ResultDataHelper::api(422,'用户id不能为空');
        }
        $member = Member::find()->where(['member_id'=>$uid])->one();
        $id = Yii::$app->request->post('symbol_id');
        if (empty($id)) {
            return ResultDataHelper::api(422,'添加的产品id不能为空');
        }
        $arr = explode(',',$member->symbol);
        $delarr = explode(',',$id);
        $newarr = array_diff($arr,$delarr);
        $member->symbol = implode(',',$newarr);
        if (!$member->save()) {
            return ResultDataHelper::api(422, $this->analyErr($member->getFirstErrors()));
        }
        return ResultDataHelper::api(200,'删除成功');
    }

   /**
 * 自选列表
 */
public function actionList()
{
	    if (!Yii::$app->request->isGet) {
	        return ResultDataHelper::api(304, 'access done!');
	    }
	    $uid = Yii::$app->user->identity->member_id;
	    // echo $uid;die;
	    if (empty($uid)) {
	    	$model = Symbol::find()->asArray()->all();
	        return ResultDataHelper::api(200,'success', $model);
	    }
	    list($memberc, $member) = Member::member($uid);
	    if (empty($memberc)) {
	        return ResultDataHelper::api(422, '用户不存在');
	    }
	    $arr   = explode(',', $member->symbol);
	    foreach ($arr as $val){
	    	$m = Symbol::find()->where(['=','id',$val])->asArray()->one();
	    	$model[] = $m;
	    }
	    //$model = Symbol::find()->where(['in','id',$arr])->asArray()->all();
	    return ResultDataHelper::api(200,'ok',$model);
 }
        /**
     * 更新排序
     * @return mixed
     */
    public function actionUpdateSort()
    {
        if (!Yii::$app->request->isPost) {
            return ResultDataHelper::api(403, 'Access Done');
        }
        $uid    = Yii::$app->user->identity->member_id;
        $member = Member::find()->where(['member_id'=>$uid])->one();
        if (empty($member)) {
            return ResultDataHelper::api(422, '用户不存在');
        }
        $member_arr = explode(',', $member->symbol);
        if (empty($member_arr)) {
            return ResultDataHelper::api(422, '请先添加自选产品');
        }
        $sort     = Yii::$app->request->post('sort');
        $sort_arr = explode(',', $sort);
        if (empty($sort_arr)) {
            return ResultDataHelper::api(422, '没有排序数据');
        }
        $s1 = sort($member_arr);
        $s2 = sort($sort_arr);
        // print_r($member_arr);die;
        if ($member_arr != $sort_arr) {
            return ResultDataHelper::api(422, '数据错误无法进行排序');
        }
        $member->symbol = $sort;

        if (!$member->save()) {
            return ResultDataHelper::api(422, '更新排序失败');
        }
        return ResultDataHelper::api(200, 'success');
    }
     /**
     * 挂单转持仓
     **/
    public static function actionPendingToPosition($orderid)
    {
        try {
            if (empty($orderid)) {
                throw new \Exception('订单不能为空');
            }
            $order = OrderReason::findOne($orderid);
            if (empty($order)) {
                throw new \Exception('订单不存在');
            }
            if (!in_array($order->state, ['挂单', '完成', '删除'])) {
                throw new \Exception('订单状态不明');
            }
            if ($order->state == '完成') {
                throw new \Exception('该订单已完成');
            }
            if ($order->state == '删除') {
                throw new \Exception('该订单已删除');
            }
            //将该订单添加到普通订单表
            $model = new OrderModel();
            //删除不必要的属性
            unset($order->id);
            unset($order->limit);
            unset($order->limit_num);
            unset($order->limit_num_cal);
            unset($order->limit_time);
            unset($order->limit_price);
            $model->setAttributes($order->toArray());
            $model->open_time = date('Y-m-d h:i:s', time());
            $model->created_at = time();
            $model->updated_at = time();
            $model->state = '持仓';
            //print_r($model);die;
            if (!$model->save()) {
                return [FALSE, $model->getFirstErrors()];
            }
            // echo '<pre>';
            // print_r($model);die;
            $order->state = '完成';
            $order->updated_at = time();
            if (!$order->save()) {

                return [FALSE, $model->getFirstErrors()];
            }

            return [TRUE, '成功'];
        } catch (\Exception $e) {
            return [FALSE, $e->getMessage()];
        }
    }
}